package me.zhengjie.modules.houseshow.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.base.ApiResult;
import me.zhengjie.modules.houseshow.domain.HouseShow;
import me.zhengjie.modules.houseshow.domain.vo.GlobalVo;
import me.zhengjie.modules.houseshow.domain.vo.PanoramaVo;
import me.zhengjie.modules.houseshow.service.HouseShowService;
import me.zhengjie.modules.houseshow.service.dto.HouseShowDto;
import me.zhengjie.modules.houseshow.service.dto.HouseShowQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
* @author TianI
* @date 2021-12-24
**/
@RestController
@RequiredArgsConstructor
@CrossOrigin
@Api(tags = "HouseShowController管理")
@RequestMapping("/api/houseShow")
public class HouseShowController {

    private final HouseShowService houseShowService;

    @GetMapping("/getById/{Id}")
    @Log("根据ID查询展示数据")
    @ApiOperation("根据ID查询展示数据")
    public ResponseEntity queryById(@PathVariable Long Id){
        HouseShowDto byId = houseShowService.findById(Id);
        return new ResponseEntity<>(byId,HttpStatus.OK);
    }

    @GetMapping
    @Log("查询HouseShowController")
    @ApiOperation("查询HouseShowController")
    @PreAuthorize("@el.check('houseShow:list')")
    public ResponseEntity query(HouseShowQueryParam query, Pageable pageable){
        return new ResponseEntity<>(houseShowService.queryAll(query,pageable),HttpStatus.OK);
    }
    public static String removeHtmlTag(String inputString) {
        if (inputString == null)
            return null;
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        java.util.regex.Pattern p_script;
        java.util.regex.Matcher m_script;
        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;
        try {
            //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
            String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
            //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
            String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
            p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); // 过滤script标签
            p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); // 过滤style标签
            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); // 过滤html标签
            textStr = htmlStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return textStr;// 返回文本字符串
    }
    @PostMapping
    @Log("新增HouseShowController")
    @ApiOperation("新增HouseShowController")
    @PreAuthorize("@el.check('houseShow:add')")
    public ResponseEntity create(@Validated @RequestBody HouseShowDto resources){

        deleteHtml(resources);
        return new ResponseEntity<>(houseShowService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改HouseShowController")
    @ApiOperation("修改HouseShowController")
    @PreAuthorize("@el.check('houseShow:edit')")
    public ResponseEntity update(@Validated @RequestBody HouseShowDto resources){
        deleteHtml(resources);
        houseShowService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    private void deleteHtml(@RequestBody @Validated HouseShowDto resources) {
        resources.setGlobalContent(removeHtmlTag(resources.getGlobalContent()));
        resources.setPanoramaContent(removeHtmlTag(resources.getPanoramaContent()));
        resources.setExampleContent(removeHtmlTag(resources.getExampleContent()));
        resources.setRoamContent(removeHtmlTag(resources.getRoamContent()));
    }

    @DeleteMapping
    @Log("删除HouseShowController")
    @ApiOperation("删除HouseShowController")
    @PreAuthorize("@el.check('houseShow:del')")
    public ResponseEntity delete(@RequestBody Set<Long> ids) {
        houseShowService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping("/getGlobalById/{hsId}")
    public ApiResult<Map<String, Object>> getGlobalById(@PathVariable String hsId) {
        GlobalVo globalVo = houseShowService.getGlobalById(hsId);
        Map<String, Object> GlobalMap = new HashMap<>();
        GlobalMap.put("title",globalVo.getTitle());
        GlobalMap.put("company",globalVo.getCompany());
        GlobalMap.put("houseType",globalVo.getHouseType());
        return ApiResult.ok(GlobalMap);
    }

    @GetMapping("/getPanoramaById/{hsId}")
    public ApiResult<Map<String, Object>> getPanoramaById(@PathVariable String hsId) {
        PanoramaVo panoramaVo =houseShowService.getPanoramaById(hsId);
        Map<String, Object> PanoramaMap = new HashMap<>();
        PanoramaMap.put("menuShow",panoramaVo.getMenuShow());
        PanoramaMap.put("scenes",panoramaVo.getScenes());
        PanoramaMap.put("detail",panoramaVo.getDetail());
        PanoramaMap.put("previewPanorama",panoramaVo.getPreviewPanorama());
        PanoramaMap.put("config",panoramaVo.getConfigVo());

        return ApiResult.ok(PanoramaMap);
    }
    /*
    @Log("导出HouseShowController")
    @ApiOperation("导出HouseShowController")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('houseShow:list')")
    public void download(HttpServletResponse response, HouseShowQueryParam query) throws IOException {
        houseShowService.download(houseShowService.queryAll(query), response);
    }*/

}
